﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL_HealthInsurance;
using DTO_HealthInsurance;
using System.Data;
namespace HealthInsuranceMgm.Admin
{
    public partial class Listpolicy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                load();
                PanelUpdatePolicy.Visible = false;
                PanelListpolicy.Visible = true;
            }
        }
        public void load()
        {
            BLLPolicies bll = new BLLPolicies();
            grvPolicy.DataSource =bll.ListPolicy() ;
            grvPolicy.DataBind();
        }

        public void LoadToDropdowlist()
        {
            BLLCompany objCom = new BLLCompany();
            ddlCompanyId.DataSource = objCom.ListCompany();
            ddlCompanyId.DataTextField = "CompanyName";

            ddlCompanyId.DataValueField = "CompanyId";
            ddlCompanyId.DataBind();
        }
        public void LoadData(int PolicyId)
        {
            LoadToDropdowlist();
            BLLPolicies obj = new BLLPolicies();
            DTOPolicies objDt = new DTOPolicies();
            objDt.PolicyId = PolicyId;
            DataTable dt = obj.GetPolicyById(objDt);

            txtpolicyname.Text = dt.Rows[0]["PolicyName"].ToString();
            txtdes.Text = dt.Rows[0]["PolicyDesc"].ToString();
            txtamount.Text = dt.Rows[0]["Amount"].ToString();
            txtemi.Text = dt.Rows[0]["Emi"].ToString();
            ddlCompanyId.SelectedValue = dt.Rows[0]["CompanyId"].ToString();

            txtmedicalid.Text = dt.Rows[0]["MedicalId"].ToString();

        }
        protected void grvPolicy_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int PolicyId = (int)grvPolicy.DataKeys[e.RowIndex].Value;
            BLLPolicies obj = new BLLPolicies();
            DTOPolicies g = new DTOPolicies();
            g.PolicyId = PolicyId;
            obj.deletePolicy(g);
            load();
        }

        protected void grvPolicy_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript: return " +
                "confirm('Are you sure you want to delete this record " +
                DataBinder.Eval(e.Row.DataItem, "PolicyId") + "')");

            }
        }

        protected void grvPolicy_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            BLLPolicies obj = new BLLPolicies();
            DTOPolicies objDt = new DTOPolicies();
            if (e.CommandName == "Delete")
            {
                // get the categoryID of the clicked row
                int PolicyId = Convert.ToInt32(e.CommandArgument);
                // Delete the record 
                objDt.PolicyId = PolicyId;
                obj.deletePolicy(objDt);
                // Implement this on your own :) 
            }
        }

        protected void grvPolicy_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvPolicy.PageIndex = e.NewPageIndex;
            load();
        }
        public SortDirection SortDirection
        {
            get
            {

                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }
        protected void grvPolicy_Sorting(object sender, GridViewSortEventArgs e)
        {
            BLLPolicies obj = new BLLPolicies();
            string sortExpression = e.SortExpression;
            string Direction = "";

            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                Direction = "DESC";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                Direction = "ASC";
            }
            DataTable dt = obj.ListPolicy();
            dt.DefaultView.Sort = sortExpression + " " + Direction;
            grvPolicy.DataSource = dt;
            grvPolicy.DataBind();
        }

       
        protected void grvPolicy_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
        int PolicyId = Int32.Parse(grvPolicy.DataKeys[e.NewSelectedIndex].Value.ToString());
        LoadData(PolicyId);
          
          
        PanelListpolicy.Visible = false;
        PanelUpdatePolicy.Visible = true;
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {

        }
    }
}